<template>
  <div>
    <el-dialog title="编辑企业" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item size="mini"  label="企业名称" >
          <el-input size="mini" v-model="editForm.companyName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="企业邮箱" >
          <el-input size="mini" v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="企业地址" >
          <el-input size="mini" v-model="editForm.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="企业法人" >
          <el-input size="mini" v-model="editForm.legalPerson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="登陆密码" >
          <el-input size="mini" show-password v-model="editForm.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      height="560"
      v-loading="loading"
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%;"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="企业id">
              <span>{{ props.row.cid}}</span>
            </el-form-item>
            <el-form-item label="注册时间">
              <span>{{getTime(props.row.registerTime)}}</span>
            </el-form-item>
            <el-form-item label="企业邮箱">
              <span>{{ props.row.email}}</span>
            </el-form-item>
            <el-form-item label="法人注册时间">
              <span>{{ getTime(props.row.legalRegTime)}}</span>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" prop="companyName"></el-table-column>
      <el-table-column label="法人" prop="legalPerson"></el-table-column>
      <el-table-column label="企业地址" prop="address"></el-table-column>
      <el-table-column align="right">
        <template slot="header">
          <input
                  class="seach"
            @keyup.enter="searchHandle()"
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"
          />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <br />
    <br />
    <div style="float: right;margin-right: 130px" class="block">
      <el-pagination
      background
      hide-on-single-page
      @current-change="currentChange"
      layout="prev, pager, next"
      :current-page="page.current"
      :total="page.total"></el-pagination>
    </div>
  </div>
</template>

<script>
import { searchUnit, getUnitList, delUnit,getPageList,upCompany } from "../../network/admin";
import Loading from "../../components/common/Loading"
export default {
  name: "CompanyList",
  data() {
    return {
      dialogFormVisible:false,
      editForm:{
        id:'',
        companyName:'',
        email:'',
        address:'',
        legalPerson:'',
        password:''
      },
      search: "",
      loading:true,
      page: {
        //后端传过来的数据,包含total(总条目),pages(总页数),records(数据),current(当前页号)
      }
    };
  },
  computed: {
    tableData() {
      console.log(this.page.records+"漂亮")
      return this.page.records;
    }
  },
  methods: {
    getTime(time){
      let dt=new Date(time)
      return (
          dt.getFullYear() +
          "-" +
          (dt.getMonth() + 1) +
          "-" +
          dt.getDate()
      );
    },
    getData(){
      getUnitList("company")
              .then(res => {
                this.loading = false;
                this.page = res.data.extend.page;
                console.log("获取数据"+res)
                console.log(res)
              })
              .catch(err => {
                console.log(err);
              });
    },
    currentChange(val){
      getPageList('company',val)
      .then(res=>{
        this.page=res.data.extend.page
      })
    },
    change(e) {
      this.$forceUpdate();
    },
    searchHandle() {
      searchUnit("company", this.search).then(res => {
        console.log(res);
        this.page = res.data.extend.page;
      });
    },
    handleEdit(index,row){
      this.dialogFormVisible=true;
      this.editForm=row
    },
    editSubmit(){
      upCompany(this.editForm)
      .then(res=>{
        console.log(res)
        this.dialogFormVisible=false
        this.$message.success(res.data.extend.msg)
      })
    },
    handleDelete(index, row) {
      delUnit("company", row.cid).then(res => {
        this.$message(res.data.extend.msg);
        this.getData()
      });
    }
  },
  created() {
    this.getData()
  }
};
</script>

<style scoped>
  .seach{
    width: 240px;
    height: 24px;
    outline: none;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
